package com.yiqi.zhiyuan.feature.certification.presenter;

import android.text.TextUtils;

import com.suozhang.framework.framework.AM;
import com.suozhang.framework.utils.ValidatorUtil;
import com.yiqi.zhiyuan.R;
import com.yiqi.zhiyuan.entity.dto.DiverCertificationDTO;
import com.yiqi.zhiyuan.entity.vo.CertufucateInfoVO;
import com.yiqi.zhiyuan.feature.certification.contract.CertificationContract;
import com.yiqi.zhiyuan.feature.certification.dependencies.certification.DaggerCertificationPresenterComponent;

import javax.inject.Inject;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

/**
 * @ProjectName: ZhiYuan
 * @Description: java类作用描述
 * @Author: lijuwen
 * @CreateDate: 2019/3/14 10:54
 * @UpdateUser: 更新者
 * @UpdateDate: 2019/3/14 10:54
 * @UpdateRemark: 更新说明
 * @Version: 1.0
 */
public class CertificationPresenter implements CertificationContract.Presenter {

    private CertificationContract.View mView;

    @Inject
    CertificationContract.Model mModel;


    public CertificationPresenter(CertificationContract.View mView) {
        this.mView = mView;
        DaggerCertificationPresenterComponent.create().inject(this);
    }


    @Override
    public void uploadInfo(String realName, String sex, String idCard, String driverLicense, String bankCard,
                           String carNo, String carName, String carModelNo, String carColor) {



        if (!ValidatorUtil.isIDCard(idCard)) {
            mView.showUploadInfoError(AM.res().getString(R.string.toast_certification_idacrd_error));
            return;
        }

        if (!ValidatorUtil.isIDCard(driverLicense)) {
            mView.showUploadInfoError(AM.res().getString(R.string.toast_certification_license_error));
            return;
        }


        if (!ValidatorUtil.checkBankCard(bankCard)) {
            mView.showUploadInfoError(AM.res().getString(R.string.toast_certification_bank_error));
            return;
        }
        if (!ValidatorUtil.isCarnumberNO(carNo)) {
            mView.showUploadInfoError(AM.res().getString(R.string.toast_certification_carno_error));
            return;
        }


        if (TextUtils.isEmpty(carName)) {
            mView.showUploadInfoError(AM.res().getString(R.string.toast_certification_brand_error));
            return;
        }

        if (TextUtils.isEmpty(carModelNo)) {
            mView.showUploadInfoError(AM.res().getString(R.string.toast_certification_type_error));
            return;
        }

        if (TextUtils.isEmpty(carColor)) {
            mView.showUploadInfoError(AM.res().getString(R.string.toast_certification_color_error));
            return;
        }
        DiverCertificationDTO certificationDTO = new DiverCertificationDTO();
        certificationDTO.setRealName(realName);
        certificationDTO.setSex(sex);
        certificationDTO.setIdCard(idCard);
        certificationDTO.setDriverLicense(driverLicense);
        certificationDTO.setBankCard(bankCard);
        certificationDTO.setCarNo(carNo);
        certificationDTO.setCarName(carName);
        certificationDTO.setCarModelNo(carModelNo);
        certificationDTO.setCarColor(carColor);

        mView.showUploadInfoSuccess(certificationDTO);
    }

    @Override
    public void queryAuthInfo() {
        mModel.doQueryAuthInfo()
                .compose(mView.bindToLife())
                .subscribe(new Observer<CertufucateInfoVO>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        mView.showLoading(d);
                    }

                    @Override
                    public void onNext(CertufucateInfoVO s) {
                        mView.showAuthInfoSuccess(s);
                        mView.dismissLoading();
                    }

                    @Override
                    public void onError(Throwable e) {
                        mView.dismissLoading();
                        mView.showErrorMsg(e.getMessage());
                    }

                    @Override
                    public void onComplete() {
                        mView.dismissLoading();
                    }
                });
    }

    @Override
    public void start() {

    }
}
